package moodish.comm;

import java.io.IOException;

import moodish.dummy.ClientCommDummy;
import moodish.message.MessageFromServer;

/**
 * Thread started by a Client to listen messages from the server.
 * The client uses this Thread to receive messages from the server.
 * This will execute until this thread gives an IOException due to the server has closed the socket.
 *  
 */
public class ListenMessagesFromServer implements Runnable {
	private ClientCommDummy cmd;
	
	public ListenMessagesFromServer(ClientCommDummy cmd)
	{
		this.cmd = cmd;
	}

	@Override
	public void run() {
		// TODO Auto-generated method stub
		while (true) {
			try {
				MessageFromServer messageFromServer = (MessageFromServer) cmd.getFromServer()
						.readObject();
				cmd.addMessage(messageFromServer);
			} catch (ClassNotFoundException e) {
				e.printStackTrace();
			} catch (IOException e) {
				break;
			}

		}
	}

}
